﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
// using Chat;

namespace Chat.Client.GUI
{
    /// <summary>
    /// Dialog to accept:
    /// - user nick name
    /// - user preferred color
    /// - chat server ip (from the base dialog)
    /// - chat server port number (from the base dialog)
    /// </summary>
    public partial class ConnectToDialog : Chat.GUI.IPEndPointDialog
    {
        public ConnectToDialog()
        {
            InitializeComponent();
        }

        /// <summary>
        /// The dialog data model.
        /// </summary>
        private LoginData login;

        /// <summary>
        /// Gets or sets the dialog data model.
        /// </summary>
        public LoginData Login
        {
            get { return login; }
            protected set { login = value; }
        }

        /// <summary>
        /// Validates the values which are currently on controls of the parent dialod.
        /// Then validates the value for the nickname which is currently on the corresponding control of the dialog.
        /// </summary>
        /// <returns>
        /// true if _all_ the values which are currently on controls of the parent
        /// dialog and on controls of this dialog are valid; false - otherwise.
        /// </returns>
        protected override bool ValidateData()
        {
            //
            // validate the parent controls first.
            bool isError = !base.ValidateData();

            //
            // then validate my controls

            NickName nick = null;

            try
            {
                //
                // The NickName ctor throws exception when try to initialize with bad nickname value
                nick = new NickName(this.txtNickName.Text);
                ClearError(this.txtNickName);
            }
            catch (NickNameException ex)
            {
                isError = true;
                ShowErrorMsg(this.txtNickName, ex.Message);
            }

            return !isError;
        }

        /// <summary>
        /// Updates the data model of the parent dialog with the corrsponding values from
        /// the parent dialog controls.
        /// Then updates the data model this sialog with the value of the nickname, which
        /// is currently on the corresponding dialog control.
        /// </summary>
        protected override void UpdateModel()
        {
 	        base.UpdateModel();

            NickName nick = new NickName(this.txtNickName.Text);

            if (Login == null)
            {
                Login = new LoginData(nick, this.colorDialog.Color);
            }
            else
            {
                Login.User = nick;
                Login.TextColor = this.colorDialog.Color;
            }
        }

        /// <summary>
        /// Restores the values of the parent dialog control from the corrsponding
        /// values of the parent dialog data model.
        /// If the restore of parent dialog controls succeded, then restores the values
        /// of this dialog controls (nickname control) from the corresponding values
        /// of this dialog data model.
        /// </summary>
        /// <returns>
        /// true, if the parent dialog controls were restored and the model for this
        /// dialog exists (not null); false - otherwise.
        /// </returns>
        protected override bool RestoreFromModel()
        {
            if (!base.RestoreFromModel())
                return false;

            if (Login == null)
                return false;
            
            this.txtNickName.Text = Login.User.Value;
            this.colorDialog.Color = Login.TextColor;

            return true;
        }

        private void btnSelectColor_Click(object sender, EventArgs e)
        {
            DialogResult dr = this.colorDialog.ShowDialog();

            if (dr == DialogResult.OK)
            {
                this.lblAaBbCcXxYyZz.ForeColor = this.colorDialog.Color;
            }
        }
    }
}
